/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.drug.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.drug.domain.DrugInfo;
import com.jeeplus.drug.service.DrugInfoService;
import com.jeeplus.drug.service.dto.DrugInfoDTO;
import com.jeeplus.drug.service.mapstruct.DrugInfoWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 药品信息Controller
 * @author xx
 * @version 2023-12-30
 */

@Api(tags ="药品信息")
@RestController
@RequestMapping(value = "/drug/drugInfo")
public class DrugInfoController {

	@Autowired
	private DrugInfoService drugInfoService;

	@Autowired
	private DrugInfoWrapper drugInfoWrapper;

	/**
	 * 药品信息列表数据
	 */
	@ApiLog("查询药品信息列表数据")
	@ApiOperation(value = "查询药品信息列表数据")
	@PreAuthorize("hasAuthority('drug:drugInfo:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<DrugInfo>> list(DrugInfoDTO drugInfoDTO, Page<DrugInfo> page) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (drugInfoDTO, DrugInfoDTO.class);
		IPage<DrugInfo> result = drugInfoService.page (page, queryWrapper);
		return ResponseEntity.ok (result);
	}

	@GetMapping("listAll")
	public ResponseEntity listAll(DrugInfoDTO drugInfoDTO) throws Exception {
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (drugInfoDTO, DrugInfoDTO.class);
		List<DrugInfoDTO> drugInfoDTOs = drugInfoService.list(queryWrapper);
		return ResponseEntity.ok (drugInfoDTOs);
	}


	/**
	 * 根据Id获取药品信息数据
	 */
	@ApiLog("根据Id获取药品信息数据")
	@ApiOperation(value = "根据Id获取药品信息数据")
	@PreAuthorize("hasAnyAuthority('drug:drugInfo:view','drug:drugInfo:add','drug:drugInfo:edit')")
	@GetMapping("queryById")
	public ResponseEntity<DrugInfoDTO> queryById(String id) {
		return ResponseEntity.ok ( drugInfoWrapper.toDTO ( drugInfoService.getById ( id ) ) );
	}

	/**
	 * 保存药品信息
	 */
	@ApiLog("保存药品信息")
	@ApiOperation(value = "保存药品信息")
	@PreAuthorize("hasAnyAuthority('drug:drugInfo:add','drug:drugInfo:edit')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody DrugInfoDTO drugInfoDTO) {
		//新增或编辑表单保存
		drugInfoService.saveOrUpdate (drugInfoWrapper.toEntity (drugInfoDTO));
        return ResponseEntity.ok ( "保存药品信息成功" );
	}


	/**
	 * 删除药品信息
	 */
	@ApiLog("删除药品信息")
	@ApiOperation(value = "删除药品信息")
	@PreAuthorize("hasAuthority('drug:drugInfo:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        drugInfoService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除药品信息成功" );
	}
	
	/**
     * 导出药品信息数据
     *
     * @param drugInfoDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出药品信息数据")
    @PreAuthorize("hasAnyAuthority('drug:drugInfo:export')")
    @GetMapping("export")
    public void exportFile(DrugInfoDTO drugInfoDTO, Page <DrugInfo> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (drugInfoDTO, DrugInfoDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List < DrugInfo> result = drugInfoService.page ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( drugInfoService, drugInfoWrapper ).exportExcel ( result,  options.getSheetName ( ), DrugInfoDTO.class, fileName,options.getExportFields (), response );
    }

    /**
     * 导入药品信息数据
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('drug:drugInfo:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( drugInfoService, drugInfoWrapper ).importExcel ( file, DrugInfoDTO.class );
        return ResponseEntity.ok ( result );
    }

    /**
     * 下载导入药品信息数据模板
     *
     * @param response
     * @return
     */
    @PreAuthorize ("hasAnyAuthority('drug:drugInfo:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "药品信息数据导入模板.xlsx";
        List<DrugInfoDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( drugInfoService, drugInfoWrapper ).exportExcel ( list,  "药品信息数据", DrugInfoDTO.class, fileName, null, response );
    }

}
